<script setup>
import {
    Edit,
    Delete,
    User
} from '@element-plus/icons-vue'

import { userListService } from '@/api/user';
import { ref } from 'vue';

const userUsername = ref('')


//用户列表
const users = ref([])
// const userList = async () => {
//     let result = await userListService();
//     users.value = result.data
// }
// userList()

//控制弹窗
const dialogVisible = ref(false)

//添加信息数据模型
const userModel = ref({})

//所有用户信息列表
import { userInfoService, deleteUserByIdService } from '@/api/user';
const showUserInfo = async (row) => {
    console.log(row.id)
    dialogVisible.value = true;
    let result = await userInfoService(row.id);
    userModel.value = result.data;

}


//分页条数据模型
const pageNum = ref(1)//当前页
const pageSize = ref(3)//每页条数
const total = ref(20)//总条数

//当每页条数发生了变化，调用此函数
const onSizeChange = (size) => {
    pageSize.value = size
    userPageList()
}
//当前页码发生变化，调用此函数
const onCurrentChange = (num) => {
    pageNum.value = num
    userPageList()
}

//分页列表
import { userPageService } from '@/api/user';
const userPageList = async() => {
    let params = {
        pageNum: pageNum.value,
        pageSize: pageSize.value,
        username: userUsername.value? userUsername.value: null
    }
    let result = await userPageService(params)
    total.value = result.data.total || 0
    users.value = result.data.records
}
userPageList()


//删除分类
import { ElMessage, ElMessageBox } from 'element-plus';
const deleteUser = (row) => {
    //提示，用户
    ElMessageBox.confirm(
        '是否确定删除该用户',
        '温馨提示',
        {
            confirmButtonText: '确认删除',
            cancelButtonText: '取消',
            type: 'warning',
        }
    )
        .then(async () => {
            //调用接口
            let result = await deleteUserByIdService(row.id);
            ElMessage({
                type: 'success',
                message: '成功删除',
            })
            //更新
            userList();
        })
        .catch(() => {
            ElMessage({
                type: 'info',
                message: '取消删除',
            })
        })
}

//是否有删除按钮——管理员身份 可删除 用户
import useUserInfoStore from '@/stores/userInfo';
const userInfoStore = useUserInfoStore();
const userInfo = ref({ ...userInfoStore.info })
const showBtn = (row) => {
    if(row.isAdmin === 1 || userInfo.value.isAdmin === 0){
        return false;
    }
    return true;
}

//重置按钮
const clearBtn = () =>{
    userUsername.value = ''
    userPageList()
}

</script>
<template>
    <el-card class="page-container">
        <template #header>
            <div class="header">
                <span style="font-weight: bolder;">用户管理</span>
            </div>
        </template>
        <!-- 搜索表单 -->
        <el-form inline>
            <el-form-item label="用户名:">
                <el-input v-model="userUsername" style="width: 200px"></el-input>
            </el-form-item>

            <el-form-item>
                <el-button type="primary" @click="userPageList">搜索</el-button>
                <el-button @click="clearBtn">重置</el-button>
            </el-form-item>
        </el-form>
        <!-- 用户列表 -->
        <el-table :data="users">
            <el-table-column label="用户名" width="150" prop="username"></el-table-column>
            <el-table-column label="年龄" prop="age"> </el-table-column>
            <el-table-column label="性别" prop="sex">
                <template #default="{ row }">
                    <p>{{ row.sex === 0 ? '男' : '女' }}</p>
                </template>
            </el-table-column>

            <el-table-column label="邮箱" prop="email"></el-table-column>
            <el-table-column label="注册时间" prop="createTime"> </el-table-column>
            <el-table-column label="更新时间" prop="updateTime"> </el-table-column>
            <el-table-column label="操作" width="100">
                <template #default="{ row }">
                    <el-button :icon="User" circle plain type="primary" @click="showUserInfo(row)"></el-button>
                    <el-button :icon="Delete" circle plain type="danger" @click="deleteUser(row)"
                    v-if="showBtn(row)"></el-button>
                </template>
            </el-table-column>
            <template #empty>
                <el-empty description="没有数据" />
            </template>
        </el-table>
        <!-- 分页条 -->
        <el-pagination v-model:current-page="pageNum" v-model:page-size="pageSize" :page-sizes="[3, 5, 10, 15]"
            layout="jumper, total, sizes, prev, pager, next" background :total="total" @size-change="onSizeChange"
            @current-change="onCurrentChange" style="margin-top: 20px; justify-content: flex-end" />
        <!-- 添加详情弹窗 -->
        <el-dialog v-model="dialogVisible" title="信息详情" width="45%">
            <el-form :model="userModel" label-width="100px" size="large">
                <p style="font-weight: bolder;" v-if="userInfo.isAdmin===1">>>>{{ userModel.isAdmin===0?'用户':'管理员' }}<<<</p>
                <el-form-item label="用户名" prop="username">
                    <el-input v-model="userModel.username" disabled></el-input>
                </el-form-item>
                <el-form-item label="用户昵称" prop="nickname">
                    <el-input v-model="userModel.nickname" disabled></el-input>
                </el-form-item>
                <el-form-item label="用户性别" prop="sex">
                    <el-input :value="userModel.sex===0?'男':'女'" disabled></el-input>
                </el-form-item>
                <el-form-item label="用户年龄" prop="age">
                    <el-input v-model="userModel.age" disabled></el-input>
                </el-form-item>
                <el-form-item label="用户邮箱" prop="email">
                    <el-input v-model="userModel.email" disabled></el-input>
                </el-form-item>
                <el-form-item label="用户手机号" prop="phone">
                    <el-input v-model="userModel.phone" disabled></el-input>
                </el-form-item>
                <el-form-item label="用户地址" prop="address">
                    <el-input v-model="userModel.address" disabled></el-input>
                </el-form-item>
                <el-form-item label="用户爱好" prop="hobby">
                    <el-input v-model="userModel.hobby" style="width: 240px" :rows="5" type="textarea" disabled />
                </el-form-item>
                <el-form-item label="创建时间" prop="createTime">
                    <el-input v-model="userModel.createTime" disabled></el-input>
                </el-form-item>
                <el-form-item label="修改时间" prop="updateTime">
                    <el-input v-model="userModel.updateTime" disabled></el-input>
                </el-form-item>
            </el-form>
            <template #footer>
                <span class="dialog-footer">
                    <el-button @click="dialogVisible = false">关闭</el-button>
                </span>
            </template>
        </el-dialog> 

    </el-card>
</template>
<style lang="scss" scoped>
.page-container {
    min-height: 100%;
    box-sizing: border-box;

    .header {
        display: flex;
        align-items: center;
        justify-content: space-between;
    }
}


.editor {
    width: 100%;

    :deep(.ql-editor) {
        min-height: 200px;
    }
}
</style>